//
//  MCWebViewController.m
//  WisdomSpace
//
//  Created by goulela on 2017/9/11.
//  Copyright © 2017年 MC. All rights reserved.
//

#import "MCWebViewController.h"

#import <JavaScriptCore/JavaScriptCore.h>

#import "MBProgressHUD.h"


@interface MCWebViewController () <UIWebViewDelegate>

{
    NSInteger _isFirstShow;
    UIWebView * _webView;
}
@property (strong, nonatomic) JSContext *context;

@end

@implementation MCWebViewController

#pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self basicSetting];
}


#pragma mark - 系统代理

#pragma mark - 点击事件
- (void)leftItemClikced {
    
    if (_webView.canGoBack) {
        [_webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


#pragma mark - 实现方法
#pragma mark 基本设置
- (void)basicSetting
{
    self.title = self.titleStr;
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setBackgroundImage:[UIImage imageNamed:@"Reuse_back"] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button addTarget:self action:@selector(leftItemClikced) forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenHighlighted = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadWebView];
}




#pragma mark 加载首页webView
-(void)loadWebView{
    
    CGFloat MCSafeAreaBottomHeight = 0;
    if ([UIScreen mainScreen].bounds.size.height == 812) {
        MCSafeAreaBottomHeight = 34;
    }
    
    UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64 - MCSafeAreaBottomHeight)];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
    
    [webView loadRequest:request];
    
    webView.delegate = self;
    [self.view addSubview:webView];
    _webView = webView;
}


- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:true];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{  // h5 加载完毕
  
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    [UIApplication sharedApplication].networkActivityIndicatorVisible =NO;
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
    
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    __block typeof(self) weakSelf = self;

    self.context[@"h5Finish"] = ^() {  // 店铺信息
        [weakSelf.navigationController popViewControllerAnimated:true];
    };
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{  // h5 加载失败
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}



@end
